package com.ihighest.app.common.api;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.ToastKit;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

/**
 * Api 回调接口
 * 
 * @author Vity
 * @date 2015-5-13
 * @mail vity.1215@gmail.com
 */
public abstract class ApiCallback extends RequestCallBack<String> {

	protected static final String TAG = "Api";

	protected Context mContext;

	/* 自定义进度条 */
	boolean customProgress;

	/* 请求等待对话框 */
	ProgressDialog dialog;

	/* 数据文件最后修改时间 */
	String lastModified;

	public ApiCallback(Context context) {
		this.mContext = context;
		this.customProgress = false;
	}

	public ApiCallback(Context context, boolean customProgress) {
		this.mContext = context;
		this.customProgress = customProgress;
	}

	@Override
	public void onStart() {
		super.onStart();
		if (!customProgress) {
			try {
				dialog = ProgressDialog.show(mContext, "正在请求网络数据", "请等候", true);
			} catch (Exception ex) {
				Log.d(TAG, ex.getMessage(), ex);
			}
		}
	}

	@Override
	public void onFailure(HttpException e, String msg) {
		if (!customProgress && dialog != null) {
			try {
				dialog.dismiss();
			} catch (Exception ex) {
				Log.d(TAG, ex.getMessage(), ex);
			}
		}
		// Token失效处理
		if (e.getExceptionCode() == 403) {
			AppKit.cleanUserInfo(mContext);
		} else if (e.getExceptionCode() == 500) {
			if (mContext instanceof Activity) {
				ToastKit.showShort(mContext, "服务器繁忙，请稍后再试");
			} else {
				ToastKit.showShort(mContext, "服务器繁忙，请稍后再试");
			}
		} else if (e.getExceptionCode() == 401) {
			if (mContext instanceof Activity) {
				ToastKit.showShort(mContext, "请求失败");
			} else {
				ToastKit.showShort(mContext, "请求失败");
			}
		} else if (e.getExceptionCode() == 400) {
			if (mContext instanceof Activity) {
				ToastKit.showShort(mContext, "请求失败");
			} else {
				ToastKit.showShort(mContext, "请求失败");
			}
		} else if (e.getExceptionCode() == 504) {
			if (mContext instanceof Activity) {
				ToastKit.showShort(mContext, "服务器响应超时");
			} else {
				ToastKit.showShort(mContext, "服务器响应超时");
			}
		} else if (e.getExceptionCode() == 404) {
			// pass
		}
		handleFailure(e, "网络请求失败");
		Log.e(TAG, "HTTP Code: " + e.getExceptionCode() + "  MSG: " + msg, e);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onSuccess(ResponseInfo<String> responseInfo) {
		Log.d(TAG, "Result:" + responseInfo.result);
		if (responseInfo.getFirstHeader("Last-Modified") != null) {
			lastModified = responseInfo.getFirstHeader("Last-Modified").getValue();
		} else if (responseInfo.getFirstHeader("ETag") != null) {
			lastModified = responseInfo.getFirstHeader("ETag").getValue();
		} else if (responseInfo.getFirstHeader("Date") != null) {
			lastModified = responseInfo.getFirstHeader("Date").getValue();
		}
		if (!customProgress && dialog != null) {

			if (dialog.isShowing()) {

				try {

					dialog.dismiss();
				} catch (Exception e) {

				}
			}
		}
		try {
			ApiResult result = new ApiResult(responseInfo);
			handleSuccess(result);
		} catch (Exception e) {
			Log.e(TAG, e.getMessage(), e);
			handleFailure(e, "遇到火星文了!-_-");
		}
	}

	/**
	 * 处理请求失败
	 * 
	 * @param e
	 * @param msg
	 */
	public abstract void handleFailure(Exception e, String msg);

	/**
	 * 处理请求成功
	 * 
	 * @param result
	 */
	public abstract void handleSuccess(ApiResult result);

	/**
	 * 只针对data方式调用有效
	 * 
	 * @return
	 * 
	 * @author Vity Created at 2015-11-3 下午8:32:03
	 */
	public String getLastModified() {
		return lastModified;
	}
}
